//
//  ChangePhoneController.m
//  YIChuSeller
//
//  Created by xsj on 2018/11/23.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXChangePhoneController.h"
#import "LoginAndResetManager.h"
@interface GXChangePhoneController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (strong,nonatomic) NSDictionary * userDic;

@end

@implementation GXChangePhoneController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kGlobalbgColor;
    self.title = @"更换手机号";
    self.userDic = [NSDictionary dictionaryWithDictionary:[AccountTool sharedAccountTool].account.users];
    self.phoneLabel.text = [NSString stringWithFormat:@"当前手机号码:%@",self.userDic[@"phone"]];
    
}
- (IBAction)pressPhoneCodeBtn:(UIButton *)sender {
    if (![self checkPhoneTextFieldValue]) {
        return;
    }
}

- (IBAction)pressChangePhoneNumberBtn:(UIButton *)sender {
    if (![self checkCodeTextFieldValue]) {
        return;
    }
    if (![self checkPhoneTextFieldValue]) {
        return;
    }
    
    NSString * userId = [NSString stringWithFormat:@"%@",self.userDic[@"userId"]];
    [LoginAndResetManager changePhoneNumberWithPhone:self.phoneTF.text UserId:userId success:^(id responseObject) {
        if ([responseObject[@"cod"] isEqualToString:@"001"]) {
            [SVProgressHUD showSuccessWithStatus:@"修改成功"];
            [[NSUserDefaults standardUserDefaults]setObject:self.phoneTF.text forKey:@"phoneNumber"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [SVProgressHUD showErrorWithStatus:responseObject[@"describa"]];
        }
    } fail:^(id responseObject) {
    }];
}
- (BOOL)checkPhoneTextFieldValue
{
    if ([NSString isBlankString:self.phoneTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"手机号不能为空"];
        return NO;
    }
    if (self.phoneTF.text.length !=11) {
        [SVProgressHUD showErrorWithStatus:@"输入手机号不正确"];
        return NO;
    }
    return YES;
}
- (BOOL)checkCodeTextFieldValue
{
    if ([NSString isBlankString:self.codeTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"验证码不能为空"];
        return NO;
    }
    if (self.codeTF.text.length > 6) {
        [SVProgressHUD showErrorWithStatus:@"证码的长度不超过6"];
        return NO;
    }
    return YES;
}

@end
